﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Win32;

namespace NiceControls
{
    [ToolboxItem(false)]
    public partial class NPopup : ToolStripDropDown
    {
        private readonly Control childControl;
        private bool closed = true;

        public NPopup() : base()
        {
            Padding = new Padding(1);
            ImeMode = ImeMode.Disable;
        }
        public NPopup(Control childControl) : this()
        {
            this.childControl = childControl;
            childControl.Dock = DockStyle.Fill;
            /*
             * ToolStripDropDown的Items可以通过ToolStripControlHost放置自定义控件
             */
            ToolStripControlHost toolStripControlHost = new ToolStripControlHost(childControl)
            {
                Padding = Padding.Empty,
                Margin = Padding.Empty
            };
            this.Items.Add(toolStripControlHost);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            Point point = new Point(MousePosition.X, MousePosition.Y);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                var cp = base.CreateParams;

                return cp;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Escape)
            {
                Close();
            }
        }


        protected override void OnLostFocus(EventArgs e)
        {
            Console.WriteLine("OnLostFocus");
            base.OnLostFocus(e);
            User32.SetCapture(Handle);
        }

        /// <summary>
        /// 此方法已废弃 请调用Show(Control parent);
        /// </summary>
        public new void Show()
        {
            throw new Exception("废弃的方法");
        }
        public void Show(Control parent)
        {
            Point mPoint = new Point(0, 0);
            Point point = new Point(0, 0);

            if (point.Y + parent.Height + childControl.Height > Screen.PrimaryScreen.Bounds.Height)
            {
                mPoint.Y = point.Y - childControl.Height + 1;
            }
            else
            {
                mPoint.Y = point.Y + parent.Height - 1;
            }
            mPoint.X = point.X;
            Show(parent, mPoint);
        }

        public new void Show(Control parent, Point point)
        {
            Show(parent, point.X, point.Y);
        }

        public new void Show(Control parent, int x, int y)
        {
            base.Show(parent, x, y);
            closed = false;
        }

        protected override void OnClosing(ToolStripDropDownClosingEventArgs e)
        {
            Capture = false;
            base.OnClosing(e);
        }

        protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
        {
            base.OnClosed(e);
            closed = true;
            //  Console.WriteLine("已关闭");
        }


        public new void Close(ToolStripDropDownCloseReason reason)
        {
            //   Console.WriteLine("调用了关闭方法");
            base.Close(reason);
        }
        public new void Close()
        {
            //    Console.WriteLine("调用了关闭方法");
            if (closed)
                return;
            base.Close();
        }

    }
}
